package io.vov.vitamio.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

public class SampleMediaController extends BaseMediaController {

	private ProgressBar mLoadingProgressBar;
	private ImageView mFullScreenView; // ȫ����ť

	public SampleMediaController(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public SeekBar getProgressBar() {
		return (SeekBar) findViewById("mediacontroller_seekbar");
	}

	@Override
	public View onCreateView() {
		View view = inflate(getContext(),
				getLayoutId("vitamio_small_mediacontroller"), this);
		mLoadingProgressBar = (ProgressBar) findViewById("mediacontroller_progressbar");
		mFullScreenView = (ImageView) findViewById("mediacontroller_enlarge_video");
		return view;
	}

	@Override
	public TextView getEndTimeView() {
		return (TextView) findViewById("mediacontroller_time_total");
	}

	@Override
	public TextView getStartTimeView() {
		return (TextView) findViewById("mediacontroller_time_current");
	}

	@Override
	public ImageView getPlayOperationView() {
		return (ImageView) findViewById("mediacontroller_play_pause");
	}

	@Override
	public int getPlayImageResource() {
		return getImageResourcsId("vitamio_mediacontroller_play");
	}

	@Override
	public int getPauseImageResource() {
		return getImageResourcsId("vitamio_mediacontroller_pause");
	}

	@Override
	public void show() {
		if (mPlayOperationView.getVisibility() == View.GONE
				&& mLoadingProgressBar.getVisibility() != View.VISIBLE) {
			mPlayOperationView.setVisibility(View.VISIBLE);
		}

		Animation anim = new AlphaAnimation(0, 1);
		anim.setDuration(200);
		anim.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				setVisibility(View.VISIBLE);
			}
		});
		startAnimation(anim);
	}

	@Override
	public void hide() {
		Log.d(VIEW_LOG_TAG, "hide()");
		Animation anim = new AlphaAnimation(1, 0);
		anim.setDuration(200);
		anim.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				setVisibility(View.GONE);
			}
		});
		startAnimation(anim);

	}

	@Override
	public void showLoading() {
		mLoadingProgressBar.setVisibility(View.VISIBLE);
		mPlayOperationView.setVisibility(View.GONE);
	}

	@Override
	public void hideLoading() {
		mLoadingProgressBar.setVisibility(View.GONE);
		mPlayOperationView.setVisibility(View.VISIBLE);
	}

	public View findViewById(String name) {
		try {
			int id = getResources().getIdentifier(name, "id",
					getContext().getPackageName());
			return findViewById(id);

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public int getLayoutId(String name) {
		try {
			int id = getResources().getIdentifier(name, "layout",
					getContext().getPackageName());
			return id;

		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	public int getImageResourcsId(String name) {
		try {
			int id = getResources().getIdentifier(name, "drawable",
					getContext().getPackageName());
			return id;

		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * ���õ��ȫ��С��ť������ȫ���¼�����
	 * 
	 * @param l
	 */
	public void setOnFullScreenViewClickListener(View.OnClickListener l) {
		mFullScreenView.setOnClickListener(l);
	}

}
